<?php
header('Content-Type: text/plain');
require_once dirname(__FILE__) . '/../../../incls/php/dbutils.class.php';
require_once dirname(__FILE__) . '/../../../incls/php/miscutils.class.php';
require_once dirname(__FILE__) . '/../../../incls/php/config.php';
session_name(SESSION);
session_start();
$myPdo = DbUtils::createPdoInst();
$action = $_POST['action'];
try {
  $myPdo->beginTransaction();
  switch ($action) {
    case 'remove':
      remove($myPdo);
      break;
    default:
      break;
  }
  $myPdo->commit();
} catch (PDOException $e) {
  echo $e->getMessage();
}

function remove($myPdo) {
  $q = json_decode(MiscUtils::getParam('q', '[]'));
  foreach ($q as $obj) {
    DbUtils::delete($myPdo, TABLE_NAME_PREFIX_PLUGIN . 'comp', $obj);
    $cond_vals = new stdClass();
    $cond_vals->c = 'comp_track_id = :v1';
    $cond_vals->v = array(':v1' => $obj->track_id);
    $tmps = DbUtils::get($myPdo, TABLE_NAME_PREFIX_PLUGIN . 'comp_prize', $cond_vals, NULL, NULL, NULL, NULL, NULL)->d;
    foreach ($tmps as &$tmp) {
      $tmp->path = iconv('UTF-8', 'GB2312', $tmp->path);
      if (file_exists($tmp->path)) {
        @unlink($tmp->path);
      }
    }
    $cond_vals = new stdClass();
    $cond_vals->c = 'comp_track_id = :v1';
    $cond_vals->v = array(':v1' => $obj->track_id);
    $tmps = DbUtils::get($myPdo, TABLE_NAME_PREFIX_PLUGIN . 'comp_pic', $cond_vals, NULL, NULL, NULL, NULL, NULL)->d;
    foreach ($tmps as &$tmp) {
      $tmp->path = iconv('UTF-8', 'GB2312', $tmp->path);
      if (file_exists($tmp->path)) {
        @unlink($tmp->path);
      }
    }
    $cond_vals = new stdClass();
    $cond_vals->c = 'comp_track_id = :v1';
    $cond_vals->v = array(':v1' => $obj->track_id);
    DbUtils::deleteByCond($myPdo, TABLE_NAME_PREFIX_PLUGIN . 'comp_prize', $cond_vals);
    $cond_vals = new stdClass();
    $cond_vals->c = 'comp_track_id = :v1';
    $cond_vals->v = array(':v1' => $obj->track_id);
    DbUtils::deleteByCond($myPdo, TABLE_NAME_PREFIX_PLUGIN . 'comp_pic', $cond_vals);
  }
  echo json_encode(array());
}
?>